<!doctype html>
<html lang="en" class="page-type-section">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">
<title>总体结构 - FreeMarker 手册</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="FreeMarker 手册">
<meta property="og:title" content="总体结构">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://freemarker.org/docs/dgui_template_overallstructure.html">
<link rel="canoical" href="http://freemarker.org/docs/dgui_template_overallstructure.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css">
</head>
<body itemscope itemtype="https://schema.org/Code">
    <meta itemprop="url" content="http://freemarker.org/docs/">
    <meta itemprop="name" content="FreeMarker 手册">

  <!--[if lte IE 9]>
  <div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div>
  <![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://freemarker.org" role="banner">            <img itemprop="image" src="logo.png" alt="FreeMarker">
</a><ul class="tabs"><li><a href="http://freemarker.org/">Home</a></li><li class="current"><a href="index.html">Manual</a></li><li><a class="external" href="http://freemarker.org/docs/api/index.html">Java API</a></li></ul><ul class="secondary-tabs"><li><a class="tab icon-heart" href="http://freemarker.org/contribute.html" title="Contribute"><span>Contribute</span></a></li><li><a class="tab icon-bug" href="https://sourceforge.net/p/freemarker/bugs/new/" title="Report a Bug"><span>Report a Bug</span></a></li><li><a class="tab icon-download" href="http://freemarker.org/freemarkerdownload.html" title="Download"><span>Download</span></a></li></ul></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="toc.html" class="navigation-header">Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="toc.html"><span itemprop="name">FreeMarker 手册</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui.html"><span itemprop="name">模板开发指南</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_template.html"><span itemprop="name">模板</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="dgui_template_overallstructure.html"><span itemprop="name">总体结构</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Alpha. index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="dgui_template_exp.html#exp_cheatsheet">Expressions</a></li><li><a href="ref_builtins_alphaidx.html">?builtins</a></li><li><a href="ref_directive_alphaidx.html">#directives</a></li><li><a href="ref_specvar.html">.spec_vars</a></li><li><a href="app_faq.html">FAQ</a></li></ul></div></div></div>    <div class="main-content site-width">
      <div class="content-wrapper">
  <div id="table-of-contents-wrapper" class="col-left">
      <script>var breadcrumb = ["FreeMarker 手册","模板开发指南","模板","总体结构"];</script>
      <script src="toc.js"></script>
      <script src="docgen-resources/main.min.js"></script>
  </div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="dgui_template.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_template_directives.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="dgui_template_overallstructure" itemprop="headline">总体结构</h1>
</div></div><p>实际上用程序语言编写的程序就是模板。
        <strong>FTL</strong> (代表FreeMarker模板语言)。
		  这是为编写模板设计的非常简单的编程语言。</p><p>模板(FTL编程)是由如下部分混合而成的：</p><ul>
          <li>
            <p><strong>文本</strong>：文本会照着原样来输出。</p>
          </li>

          <li>
            <p><strong>插值</strong>：这部分的输出会被计算的值来替换。插值由 
            <code class="inline-code">${</code> and <code class="inline-code">}</code> 所分隔(或者
            <code class="inline-code">#{</code> and <code class="inline-code">}</code>，这种风格已经不建议再使用了；<a href="ref_depr_numerical_interpolation.html">点击查看更多</a>)。</p>
          </li>

          <li>
            <p><strong>FTL 标签</strong>：FTL标签和HTML标签很相似，但是它们却是给FreeMarker的指示，
			  而且不会打印在输出内容中。</p>
          </li>

          <li>
            <p><strong>注释</strong>：注释和HTML的注释也很相似，但它们是由 
			  <code class="inline-code">&lt;#--</code> 和
            <code class="inline-code">--&gt;</code>来分隔的。注释会被FreeMarker直接忽略，
			更不会在输出内容中显示。</p>
          </li>
        </ul><p>我们来看一个具体的模板。其中的内容已经用颜色来标记了： 
		<span class="marked-text">文本</span>，
        <span class="marked-interpolation">插值</span>， <span class="marked-ftl-tag">FTL 标签</span>， <span class="marked-comment">注释</span>。为了看到可见的 
		<a href="gloss.html#gloss.lineBreak">换行符</a>，
		这里使用了 <em><span class="marked-invisible-text">[BR]</span></em>。</p>

<div class="code-wrapper"><pre class="code-block code-template"><span class="marked-text">&lt;html&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;head&gt;<em><span class="marked-invisible-text">[BR]</span></em>
  &lt;title&gt;Welcome!&lt;/title&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;/head&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;body&gt;<em><span class="marked-invisible-text">[BR]</span></em>
  <span class="marked-comment">&lt;#-- Greet the user with his/her name --&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
  &lt;h1&gt;Welcome <span class="marked-interpolation">${user}</span>!&lt;/h1&gt;<em><span class="marked-invisible-text">[BR]</span></em>
  &lt;p&gt;We have these animals:<em><span class="marked-invisible-text">[BR]</span></em>
  &lt;ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
  <span class="marked-ftl-tag">&lt;#list animals as animal&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
    &lt;li&gt;<span class="marked-interpolation">${animal.name}</span> for <span class="marked-interpolation">${animal.price}</span> Euros<em><span class="marked-invisible-text">[BR]</span></em>
  <span class="marked-ftl-tag">&lt;/#list&gt;</span><em><span class="marked-invisible-text">[BR]</span></em>
  &lt;/ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;/body&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;/html&gt;</span></pre></div><p>FTL是区分大小写的。 <code class="inline-code">list</code> 是指令的名称而 
		<code class="inline-code">List</code> 就不是。类似地 <code class="inline-code">${name}</code> 
		和 <code class="inline-code">${Name}</code> 或 <code class="inline-code">${NAME}</code> 也是不同的。</p><p>请注意非常重要的一点： <span class="marked-interpolation">插值</span> 
		仅仅可以在 <span class="marked-text">文本</span> 中使用。
		(也可以是字符串表达式；请参考 
		<a href="dgui_template_exp.html#dgui_template_exp_stringop_interpolation">后续内容</a>)</p><p><span class="marked-ftl-tag">FTL 标签</span> 不可以在其他
		<span class="marked-ftl-tag">FTL 标签</span> 
		和 <span class="marked-interpolation">插值</span>中使用。比如，
		这样做是 <em>错误</em> 的： 
		<code class="inline-code">&lt;#if &lt;#include &#39;foo&#39;&gt;=&#39;bar&#39;&gt;...&lt;/#if&gt;</code></p><p><span class="marked-comment">注释</span> 可以放在
		<span class="marked-ftl-tag">FTL 标签</span> 
		和 <span class="marked-interpolation">插值</span>中。比如：</p>

<div class="code-wrapper"><pre class="code-block code-template"><span class="marked-text">&lt;h1&gt;Welcome <span class="marked-interpolation">${user <span class="marked-comment">&lt;#-- The name of user --&gt;</span>}</span>!&lt;/h1&gt;<em><span class="marked-invisible-text">[BR]</span></em>
&lt;p&gt;We have these animals:<em><span class="marked-invisible-text">[BR]</span></em>
&lt;ul&gt;<em><span class="marked-invisible-text">[BR]</span></em>
<span class="marked-ftl-tag">&lt;#list <span class="marked-comment">&lt;#-- some comment... --&gt;</span> animals as <span class="marked-comment">&lt;#-- again... --&gt;</span> animal&gt;</span><em><span class="marked-invisible-text">[BR]</span></em></span>
<em>...</em></pre></div>  <div class="callout note">
    <strong class="callout-label">Note:</strong>

          <p>如果您已经自行尝试测试上面所有示例的话：那么也许会注意到，一些空格、
		  制表符和换行符从模板的输出中都不见了，尽管我们之前已经说了 
		  <span class="marked-text">文本</span> 是按照原样输出的。
		  现在不用为此而计较，这是由于FreeMarker的&quot;空格剥离&quot;特性在起作用，
		  它当然会自动去除一些多余的空格，制表符和换行符了。
		  这个特性在 <a href="dgui_misc_whitespace.html">后续内容</a> 中会解释到。</p>
          </div>
<div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="dgui_template.html"><span>Previous</span></a><a class="paging-arrow next" href="dgui_template_directives.html"><span>Next</span></a></div></div></div></div>      </div>
    </div>
<div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"><div class="column"><h3 class="column-header">Overview</h3><ul><li><a href="http://freemarker.org/index.html">What is FreeMarker?</a></li><li><a href="http://freemarker.org/freemarkerdownload.html">Download</a></li><li><a href="app_versions.html">Version history</a></li><li><a href="http://freemarker.org/history.html">About us</a></li><li><a itemprop="license" href="app_license.html">License</a></li></ul></div><div class="column"><h3 class="column-header">Handy stuff</h3><ul><li><a href="http://freemarker-online.kenshoo.com/">Try template online</a></li><li><a href="dgui_template_exp.html#exp_cheatsheet">Expressions cheatsheet</a></li><li><a href="ref_directive_alphaidx.html">#directives</a></li><li><a href="ref_builtins_alphaidx.html">?built_ins</a></li><li><a href="ref_specvar.html">.special_vars</a></li></ul></div><div class="column"><h3 class="column-header">Community</h3><ul><li><a href="https://github.com/nanlei/freemarker/tree/manual-zh-2.3-gae/src/manual">Chinese Manual on Github</a></li><li><a href="https://github.com/freemarker/freemarker">FreeMarker on Github</a></li><li><a href="https://twitter.com/freemarker">Follow us on Twitter</a></li><li><a href="https://sourceforge.net/p/freemarker/bugs/new/">Report a bug</a></li><li><a href="http://stackoverflow.com/questions/ask?tags=freemarker">Ask a question</a></li><li><a href="http://freemarker.org/mailing-lists.html">Mailing lists</a></li></ul></div></div><div class="col-right"><ul class="social-icons"><li><a class="github" href="https://github.com/freemarker/freemarker">Github</a></li><li><a class="twitter" href="https://twitter.com/freemarker">Twitter</a></li><li><a class="stack-overflow" href="http://stackoverflow.com/questions/ask?tags=freemarker">Stack Overflow</a></li></ul><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"><p><span class="generated-for-product">Generated for: Freemarker 2.3.23</span><span class="last-updated"> Last generated:
<time itemprop="dateModified" datetime="2015-09-18T14:38:51Z" title="Friday, September 18, 2015 2:38:51 PM GMT">2015-09-18 14:38:51 GMT</time></span></p> <p class="copyright">
© <span itemprop="copyrightYear">1999</span>–2015
<a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="http://freemarker.org">The FreeMarker Project</a>. All rights reserved. </p>
</div></div></div></body>
</html>
